#!/bin/bash
#
#

############################################################
# Help message
############################################################
function help() {
    echo "install.bash help|<script>|bin"
    echo "    help : print out this message" 
    echo "    script : execute a script in the 'scripts' directory (see list below)"
    echo "    bin : install bin scripts to the server"
    echo "Scripts and description (in suggested order)"
    echo "    (1)yum-install.bash : install the initial RPM files"
    echo "    (2)install-iptables.bash : install the new iptables configuration"
    echo "    (3)install-server.bash : install the server root directory and group"
    echo "    (4)install-db.bash : install directory and group for database server"
    echo "    (5)install-subversion.bash : install subversion"
    echo "    (6)install-mysql.bash : install msyql"
    echo "    (7)install-trac.bash : install trac"
}

############################################################
# Application entry
############################################################

DIR=(`pwd`)

COMMON_FUNC=$DIR/common-func.bash
export COMMON_FUNC

if [ ! -f $COMMON_FUNC ]
then
    echo "Please make sure that $COMMON_FUNC exists"
    exit 1
fi

if [ ! -f "install.env" ]
then
    echo "Please make sure install.env is located at $DIR"
    exit 1
fi

NUM_ARGS=$#
if ! [[ $NUM_ARGS == 1 ]];
then
    help
    exit 1
fi

source install.env
source $COMMON_FUNC

# check to see if the server name is set
if [ -z "$SERVER_NAME" ]; 
then
    echo "Must set SERVER_NAME to be non-empty. Please see $DIR/install.env file."
    exit 1
fi 

ARG=$1
case $ARG in
    help)
        help
        exit 0
        ;;
    bin)
        isRoot
        move_files bin /$SERVER_NAME/bin
        chmod 775 /$SERVER_NAME/bin
        ;;
    *)
        LOG_FILE=/tmp/$ARG.$INSTALL_DATE.log
        echo ""
        echo "Installation date: $INSTALL_DATE for *** $SERVER_NAME ***"
        prompt '> Press [ENTER] to continue or [CTRL-C] to exit'
        echo ""
        bash $SCRIPTS_DIR/$ARG | tee $LOG_FILE
        ;;
esac

echo ""
echo "$0 done. Please see log file at $LOG_FILE"
echo ""

